package gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Date;
import java.sql.Time;
import java.util.Calendar;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import client.Delivery;
import client.Main;
import client.Order;

public class Delivery_Greeting_Gui extends JFrame implements ActionListener 
{
	public JPanel panel = null;
	public JLabel title = null;
	public JRadioButton delivery = null;
	public static JRadioButton selfCollection = null;
	public ButtonGroup btnGroup = null;
	public JButton greeting = null;
	public JButton Continue = null;
	public int greetingCard = -1;  //-1: no greeting , 1 yes
	public Order order = null;
	public static int selection = -1;//1 - delivery, 2 - pickup 
	DeliveryGUI dg;
	GreetingGUI gu;
	Delivery d;
	public Payment_GUI payment_gui=null;
	int cancelGreet=0;
	
	public Object[] Delivery_Greeting_Data = null;  	
	
	public String greetText = null;		
	public String deliveryOid = null;	
	public String delivryName = null;		
	public String deliveryAddress = null;	
	public String deliveryPhone = null;	
	public String deliveryDatePlace = null;	
	public String deliveryTimePlace = null;	
	public String deliveryDateToReceive = null;	
	public String deliveryTimeToReceive = null;	
	
	public Delivery_Greeting_Gui()
	{
		this.setTitle("Greeting / Delivery");
		panel = new JPanel();
		panel.setLayout(null);
		this.setPreferredSize(new Dimension(300, 220));
		this.setLocation((int)Main.dim.getWidth()/2 - (int)this.getPreferredSize().getWidth()/2,(int)Main.dim.getHeight()/2 - (int)this.getPreferredSize().getHeight()/2);
		this.setResizable(false);
		
		this.addWindowListener(new WindowAdapter() 
        {
			public void windowClosing(WindowEvent e)
			{
				Main.cart.setEnabled(true);
				setVisible(false);			
			}
		});
		
		title = new JLabel("Greeting / Delivery");
		title.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,18));
		title.setBounds(60, 1, 200, 30);
  	  	panel.add(title);
  	  	
		delivery = new JRadioButton("Delivery");
		delivery.setBounds(20, 40, 100, 40);
		delivery.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
		delivery.addActionListener(this);
		delivery.setSelected(false);
		panel.add(delivery);
		
		selfCollection = new JRadioButton("Pick up in store");
		selfCollection.setBounds(140, 40, 160, 40);
		selfCollection.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,17));
		selfCollection.setSelected(false);
		selfCollection.addActionListener(this);
		panel.add(selfCollection);
		
		btnGroup=new ButtonGroup();
		btnGroup.add(delivery);
		btnGroup.add(selfCollection);
		
		greeting = new JButton("Add Greeting Card");
		greeting.setBounds(20, 90, 260, 40);
		greeting.addActionListener(this);
		panel.add(greeting);
		
		Continue = new JButton("Continue");
		Continue.setBounds(20, 140, 260, 40);
		Continue.addActionListener(this);
		panel.add(Continue);
		
		this.add(panel);
	  	this.pack();
	  	this.setEnabled(true);
	  	this.setVisible(true);
	}
	
	public Object[] getData()
	{
		Delivery_Greeting_Data = new Object[11];
		
		Delivery_Greeting_Data[0] = selection;
		Delivery_Greeting_Data[1] = greetingCard;
		Delivery_Greeting_Data[2] = greetText;
		Delivery_Greeting_Data[3] = deliveryOid;
		Delivery_Greeting_Data[4] = delivryName;
		Delivery_Greeting_Data[5] = deliveryAddress;
		Delivery_Greeting_Data[6] = deliveryPhone;
		Delivery_Greeting_Data[7] = deliveryDatePlace;
		Delivery_Greeting_Data[8] = deliveryTimePlace;
		Delivery_Greeting_Data[9] = deliveryDateToReceive;
		Delivery_Greeting_Data[10] = deliveryTimeToReceive;								
		
		return Delivery_Greeting_Data;
	}
	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == Continue)
		{	
			if(selection != -1)
			{						
				Main.cart.setVisible(false);						
				this.setEnabled(false);
				this.setVisible(false);
				
				payment_gui=new Payment_GUI(selection);
			}
			else 
				JOptionPane.showMessageDialog(null,"You must choose one of: 'Pick up in store' or 'Delivery' ","ERROR",JOptionPane.ERROR_MESSAGE);	
		}
		if(e.getSource() == greeting)
		{
			if (cancelGreet==0)
				gu=new GreetingGUI();
			else
			{
				greetingCard=-1;
				greeting.setText("Add Greeting Card");
				cancelGreet=0;
				JOptionPane.showMessageDialog(null, "Greeting Card canceled");
			}
			
			gu.btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					gu.dispose();
					greetingCard=-1;
				}
			});
			gu.btnSubmit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("greetingText()");
						if(!gu.txtText.getText().equals(""))
						{
							greetText=gu.txtText.getText();
							JOptionPane.showMessageDialog(null, "Greeting Saved");
							gu.btnBack.doClick();
							gu.dispose();
							greetingCard=1;
							greeting.setText("Cancel Greeting Card");
							cancelGreet=1;
						}
						else
							JOptionPane.showMessageDialog(null, "Please enter message");
				}
			});
		}
		if(e.getSource() == selfCollection)
		{	
			
			selection = 2;
			System.out.println("Pick up in store");
			JOptionPane.showMessageDialog(null,"Your order will wait for you at the nearest store");
		}
		
		if(e.getSource() == delivery)
		{	
			selection = 1;
			
			dg=new DeliveryGUI();
			
			dg.btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dg.dispose();
				}
			});
			dg.btnSubmit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("deliver()");
					if(!dg.btnrImediateOrder.isSelected() && !dg.btnrPlaceOrder.isSelected())
					{
						JOptionPane.showMessageDialog(null, "Please select delivery option");
						return;
					}
					if(dg.txtRecAdd.getText().equals("") || dg.txtRecName.getText().equals("") || dg.txtRecPhone.getText().equals(""))
					{
						JOptionPane.showMessageDialog(null, "One or more of the fields is empty");
						return;
					}
					dg.d.setRecAddress(dg.txtRecAdd.getText());
					dg.d.setRecName(dg.txtRecName.getText());
					dg.d.setRecPhone(dg.txtRecPhone.getText());
					dg.btnBack.doClick();
					deliveryAddress=dg.getDeliveryDetails().getRecAddress();
					delivryName=dg.getDeliveryDetails().getRecName();
					deliveryPhone=dg.getDeliveryDetails().getRecPhone();
					String s[]=dg.d.getDeliveryDateTime();
					deliveryDatePlace=s[0];
					deliveryTimePlace=s[1];
					deliveryDateToReceive=s[2];
					deliveryTimeToReceive=s[3];
				}
			});
			
			System.out.println("delivery");
		}
	}
	public int getSelection(){return selection;}
		
}
